import { DeleteModal } from '@/components/Modals';
import api from '@/pages/Customer/ListManagement/request';
import { createFilterColumns, createTableRequestParams } from '@/utils/table';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { PageContainer, ProTable } from '@ant-design/pro-components';
import { Button, Space, Typography } from 'antd';
import { useMemo, useRef, useState } from 'react';
import Detail from './Detail';
import Form from './Form';

function ShopListManagement() {
  const actionRef = useRef<ActionType>();

  const [isShowModal, setIsShowModal] = useState(false);
  const [isShowDetail, setIsShowDetail] = useState(false);
  const [curData, setCurData] = useState<Partial<CustomerListManagementApi.ListResponse>>({});
  const [filterList, setFilterList] = useState<Requestor.FilterResponse[]>([]);

  // request
  const getFilterList = async () => {
    const filterList = await api.getFilterList();
    setFilterList(filterList);
    return filterList;
  };
  const getListRequest = (
    params: Requestor.TableParams,
    filterList: Requestor.FilterResponse[],
  ) => {
    const requestParams = createTableRequestParams(params, filterList);
    return api.getList({ ...requestParams });
  };
  const getList = async (params: Requestor.TableParams) => {
    if (!filterList || !filterList.length) {
      const filterList = await getFilterList();
      return getListRequest(params, filterList);
    } else {
      return getListRequest(params, filterList);
    }
  };

  // method
  const setState = (
    state: boolean,
    setState: any,
    data?: CustomerListManagementApi.ListResponse,
  ) => {
    setCurData(data || {});
    setState(state);
  };
  const removeRequest = async (params: CustomerListManagementApi.DeleteParams) => {
    const data = await api.remove(params);
    if (data.success) {
      actionRef.current?.reload();
      setState(false, setIsShowModal);
    }
  };

  const onConfirmRemove = () => {
    const { id = '' } = curData;
    removeRequest([id]);
  };
  const onCancelRemove = () => {
    setState(false, setIsShowModal);
  };
  const columns = useMemo(() => {
    const filterColumns = createFilterColumns<CustomerListManagementApi.ListResponse>(filterList);
    const tableColumns: ProColumns<CustomerListManagementApi.ListResponse>[] = [
      {
        title: '客户名称',
        dataIndex: 'realname',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '手机号',
        dataIndex: 'mobileno',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '车牌号',
        dataIndex: 'licensePlateNumber',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '车架号',
        dataIndex: 'vin',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '积分',
        dataIndex: 'availablePoints',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '操作',
        hideInSearch: true,
        align: 'center',
        width: '25%',
        render(_, record) {
          return (
            <Space>
              <Typography.Link>
                <Button type="link" onClick={() => setState(true, setIsShowDetail, record)}>
                  详情
                </Button>
              </Typography.Link>
              <Typography.Link>
                <Form isEdit data={record} onSuccess={() => actionRef.current?.reload()} />
              </Typography.Link>
              <Typography.Link>
                <Button danger type="link" onClick={() => setState(true, setIsShowModal, record)}>
                  删除
                </Button>
              </Typography.Link>
            </Space>
          );
        },
      },
    ];
    return filterColumns.concat(tableColumns);
  }, [filterList]);
  return (
    <PageContainer>
      <ProTable
        bordered
        size="small"
        rowKey="id"
        options={false}
        actionRef={actionRef}
        pagination={{ defaultPageSize: 10, showSizeChanger: true }}
        toolBarRender={() => [<Form key="reload" onSuccess={() => actionRef.current?.reload()} />]}
        columns={columns}
        request={getList}
      />
      <Detail
        isShow={isShowDetail}
        data={curData}
        onCancel={() => setState(false, setIsShowDetail)}
      />
      <DeleteModal
        open={isShowModal}
        content="确认删除吗？"
        onOk={onConfirmRemove}
        onCancel={onCancelRemove}
      />
    </PageContainer>
  );
}

export default ShopListManagement;
